package com.gk.javaservice.controller;

import com.gk.javaservice.common.ServerResponse;
import com.gk.javaservice.po.Config;
import com.gk.javaservice.service.ConfigService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/Config")
public class ConfigController {

    @Autowired
    private ConfigService baseService;
    private Logger log = Logger.getLogger(this.getClass());


    //添加
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/add")
    public ServerResponse<?> add(@RequestBody Config t) {
        log.info(t);
        try {
            baseService.add(t);
            return ServerResponse.createBySuccess(t.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("值重复");
        }

    }

    //根据ID删除1个
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/delete")
    public ServerResponse<?> delete(@RequestBody Config t) {
        log.info("id:" + t.getId());
        baseService.delete(t.getId());
        return ServerResponse.createBySuccess(t.getId());

    }

    //修改信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/update")
    public ServerResponse<?> update(@RequestBody Config t) {
        log.info("id:" + t.getId());
        try {
            baseService.update(t);
            return ServerResponse.createBySuccess(baseService.hasById(t.getId()));
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("值重复");
        }
    }

    //获取所有信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasAll")
    public ServerResponse<?> hasAll() {
        log.info("in to hasAll");
        return ServerResponse.createBySuccess(baseService.hasAll());

    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasByKey")
    public ServerResponse<?> hasByKey(HttpServletRequest request) {
        log.info("in to hasByKey");
        String key = request.getParameter("key");
        return ServerResponse.createBySuccess(baseService.hasByKey(key));

    }

    //分页获取
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasAllToPage")
    public ServerResponse<?> hasAllToPage(HttpServletRequest request) {
        int page = 1;
        int limit = 20;
        try {
            page = Integer.parseInt(request.getParameter("page"));
        } catch (Exception e) {

        }
        try {
            limit = Integer.parseInt(request.getParameter("limit"));
        } catch (Exception e) {

        }
        String searchMsg = request.getParameter("searchMsg");
        if (searchMsg != null) {
            searchMsg = searchMsg.trim();
        }
        if (searchMsg == null)
            searchMsg = "";
        int offset = (page - 1) * limit;
        int queryTotal = baseService.queryTotal(searchMsg);
        int maxPage = (queryTotal / limit) + 1;
        List<Config> alist = baseService.hasAllToPage(offset, limit, searchMsg);
        HashMap<String, Object> map = new HashMap<>();
        map.put("queryTotal", queryTotal);
        map.put("maxPage", maxPage);
        map.put("page", page);
        map.put("list", alist);
        return ServerResponse.createBySuccess(map);

    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/queryTotal")
    public ServerResponse<?> queryTotal(HttpServletRequest request) {
        String searchMsg = request.getParameter("searchMsg");
        if (searchMsg != null) {
            searchMsg = searchMsg.trim();
        }
        return ServerResponse.createBySuccess(baseService.queryTotal(searchMsg));

    }
}
